import RNFS from 'react-native-fs';
import { zip, unzip, unzipAssets, subscribe } from 'react-native-zip-archive';


export default class FileUtil {


    /**
     *下载zip文件到指定目录
     *
     * @static
     * @param {*} srcPath
     * @param {*} destPath
     * @param {*} fileName
     * @param {*} progressCallBack
     * @return {Promise} 
     * @memberof FileUtil
     */
    static async downloadH5File(srcPath, destPath, fileName, progressCallBack) {

     
        var currentProgress = -1
    
        console.log("srcPath:" + srcPath);
        console.log("destPath:" + destPath);
        const options = {
          fromUrl: srcPath,
          toFile: destPath + "/" + fileName,
          background: true,
          begin: res => {
            // console.log('begin', res);
            // console.log('contentLength:', res.contentLength / 1024 / 1024, 'M');
          },
          progress: progressCallBack,
          progressInterval: 100
        };
        try {
         
          const ret = await RNFS.downloadFile(options);
          // console.log('ret=====', ret);
         
          return ret.promise;
         
        } catch (error) {
          console.log("downloadH5File error: ", error);
        }
      }



    /**
     *
     * 解压zip文件
     *
     * @static
     * @memberof FileUtil
     */
    static unzipFile(path, zipFileName){
        const sourcePath = path + "/" + zipFileName;
        const targetPath = path;
        const charset = 'UTF-8';

        console.log('unzipFIle sourePath=====', sourcePath);
        console.log('unzipFIle targetPath=====', targetPath);
        // charset possible values: UTF-8, GBK, US-ASCII and so on. If none was passed, default value is UTF-8
    
        
    
        return unzip(sourcePath, targetPath, charset)
         
    }
  
}